package com.ysxsoft.wdg.ui.activity.address;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.AddressAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.AddressBean;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class AddressListActivity extends BaseActivity {

    public static final int ADDRESS_CHOOSE = 200;

    public static void start(Context context, int code) {
        Intent intent = new Intent(context, AddressListActivity.class);
        intent.putExtra("code", code);
        context.startActivity(intent);
    }


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;
    @BindView(R.id.btn_add)
    TextView btnAdd;

    private int code = 0;
    private AddressAdapter mAdapter;
    private String uid = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_list;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("地址管理");

        Intent intent = getIntent();
        code = intent.getIntExtra("code", 0);

        uid = SharePrefUtils.getUserId();

        mAdapter = new AddressAdapter(R.layout.item_address);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(mAdapter);
        multStatusView.showLoading();
        getAddressList();
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        getAddressList();
    }

    private void getAddressList() {

        mAdapter.clear();
        mApiHelper.getAddressList(uid, new Observer<BaseBean<List<AddressBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<AddressBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    List<AddressBean> addressList = baseBean.getData();
                    mAdapter.setNewData(addressList);
                    multStatusView.showContent();
                } else {
                    multStatusView.showEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                multStatusView.showError();
            }

            @Override
            public void onComplete() {
            }
        });

        multStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.empty_retry_view:
                        AddressEditActivity.start(mContext, -1, null);
                        break;
                }
            }
        });
    }

    @Override
    protected void setListener() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (code != 0) {
                    Log.e("tag","点击了");
                    AddressBean item = mAdapter.getItem(position);
                    Intent intent = getIntent();
                    intent.putExtra("address",item);
                    setResult(RESULT_OK,intent);
                    finish();

                }
            }
        });

        mAdapter.setOnAddressEditListener(new AddressAdapter.OnAddressEditListener() {
            @Override
            public void onDelAddress(int position) {
                delAddress(mAdapter.getItem(position).getId());
            }

            @Override
            public void onEditAddress(int position) {
                AddressEditActivity.start(mContext, 0, mAdapter.getItem(position));
            }

            @Override
            public void onSetMoren(int position) {
                setMoren(mAdapter.getItem(position).getId());
            }
        });
    }

    private void setMoren(String aid) {
        mApiHelper.addressStatus(aid, new Observer<BaseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean baseBean) {
                ToastUtils.showToast(baseBean.getMs());
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    getAddressList();
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtils.showToast("请稍后再试");
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void delAddress(String aid) {
        mApiHelper.delAddress(aid, new Observer<BaseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean baseBean) {
                ToastUtils.showToast(baseBean.getMs());
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    getAddressList();
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtils.showToast("请稍后再试");
            }

            @Override
            public void onComplete() {

            }
        });
    }


    @OnClick({R.id.title_finish, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.btn_add:
                AddressEditActivity.start(mContext, -1, null);
                break;
        }
    }
}
